﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace XFPPT.NET.Models
{
 
        /// <summary>
        /// 模板列表响应类，用于封装模板列表相关的响应信息。
        /// Template list response class, used to encapsulate response information related to the template list.
        /// </summary>
        public class TemplateListResponse
        {
            /// <summary>
            /// 响应标识，指示响应的状态。
            /// Response flag, indicating the status of the response.
            /// </summary>
            public bool? flag { get; set; }

            /// <summary>
            /// 错误码，用于标识响应中可能出现的错误。
            /// Error code, used to identify possible errors in the response.
            /// </summary>
            public long? code { get; set; }

            /// <summary>
            /// 错误详情，提供关于错误的详细描述。
            /// Error details, providing a detailed description of the error.
            /// </summary>
            public string? desc { get; set; }

            /// <summary>
            /// 不用关注，预留字段。
            /// Do not pay attention, reserved field.
            /// </summary>
            public long? count { get; set; }

            /// <summary>
            /// 模板数据，包含 PPT 主题列表的相关信息。
            /// Template data, containing information related to the PPT theme list.
            /// </summary>
            public TemplateData? data { get; set; }

            /// <summary>
            /// 预留字段，目前为空。
            /// Reserved field, currently empty.
            /// </summary>
            public object? sid { get; set; }

        [JsonIgnore]
        private string realJsonstring = string.Empty;

        /// <summary>
        /// Gets or sets the real JSON string representation of the response.
        /// 获取或设置响应的真实 JSON 字符串表示。
        /// </summary>
        [JsonIgnore]
        public  string? RealJsonstring
        {
            get { return realJsonstring; }
            set
            {
                realJsonstring = value!;
                try
                {
                    DeserializeTemplateListResponse(value!);
                }
                catch (Exception)
                {
                    // 可添加日志记录等操作
                }
            }
        }

        /// <summary>
        /// 反序列化模板列表响应的 JSON 字符串到当前对象。
        /// 此方法将传入的 JSON 字符串解析为对应的属性值，并赋值给当前对象的各个属性。
        /// </summary>
        /// <param name="json">待反序列化的 JSON 字符串。</param>
        public virtual void DeserializeTemplateListResponse(string json)
        {
            try
            {
                JsonNode? jsonNode = JsonNode.Parse(json);
                if (jsonNode != null)
                {
                    try
                    {
                        if (jsonNode["flag"] != null)
                        {
                            this.flag = Convert.ToBoolean(jsonNode["flag"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["code"] != null)
                        {
                            this.code = Convert.ToInt64(jsonNode["code"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["desc"] != null)
                        {
                            this.desc = jsonNode["desc"]!.ToString();
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["count"] != null)
                        {
                            this.count = Convert.ToInt64(jsonNode["count"]!.ToString());
                        }
                    }
                    catch { }
                    if (jsonNode["data"] != null)
                    {
                        this.data = new TemplateData();
                        var dataNode = jsonNode["data"]!;
                        try
                        {
                            if (dataNode["total"] != null)
                            {
                                this.data.total = Convert.ToInt64(dataNode["total"]!.ToString());
                            }
                        }
                        catch { }
                        if (dataNode["records"] != null)
                        {
                            this.data.records = new List<TemplateRecord>();
                            foreach (var recordNode in dataNode["records"]!.AsArray())
                            {
                                var record = new TemplateRecord();
                                try
                                {
                                    if (recordNode!["templateIndexId"] != null)
                                    {
                                        record.templateIndexId = recordNode!["templateIndexId"]!.ToString();
                                    }
                                }
                                catch { }
                                try
                                {
                                    if (recordNode!["pageCount"] != null)
                                    {
                                        record.pageCount = Convert.ToInt64(recordNode!["pageCount"]!.ToString());
                                    }
                                }
                                catch { }
                                try
                                {
                                    if (recordNode!["type"] != null)
                                    {
                                        record.type = recordNode!["type"]!.ToString();
                                    }
                                }
                                catch { }
                                try
                                {
                                    if (recordNode!["color"] != null)
                                    {
                                        record.color = recordNode!["color"]!.ToString();
                                    }
                                }
                                catch { }
                                try
                                {
                                    if (recordNode!["industry"] != null)
                                    {
                                        record.industry = recordNode!["industry"]!.ToString();
                                    }
                                }
                                catch { }
                                try
                                {
                                    if (recordNode!["style"] != null)
                                    {
                                        record.style = recordNode!["style"]!.ToString();
                                    }
                                }
                                catch { }
                                if (recordNode!["detailImage"] != null)
                                {
                                    record.detailImage = new TemplateDetailImage();
                                    var detailImageStr = recordNode["detailImage"]!.ToString();
                                    JsonNode? detailImageNode = JsonNode.Parse(detailImageStr);
                                    try
                                    {
                                        if (detailImageNode!["titleCoverImageLarge"] != null)
                                        {
                                            record.detailImage.titleCoverImageLarge = detailImageNode!["titleCoverImageLarge"]!.ToString();
                                        }
                                    }
                                    catch { }
                                    try
                                    {
                                        if (detailImageNode!["titleCoverImage"] != null)
                                        {
                                            record.detailImage.titleCoverImage = detailImageNode!["titleCoverImage"]!.ToString();
                                        }
                                    }
                                    catch { }
                                    try
                                    {
                                        if (detailImageNode!["catalogueCoverImage"] != null)
                                        {
                                            record.detailImage.catalogueCoverImage = detailImageNode!["catalogueCoverImage"]!.ToString();
                                        }
                                    }
                                    catch { }
                                    try
                                    {
                                        if (detailImageNode!["chapterCoverImage"] != null)
                                        {
                                            record.detailImage.chapterCoverImage = detailImageNode!["chapterCoverImage"]!.ToString();
                                        }
                                    }
                                    catch { }
                                    try
                                    {
                                        if (detailImageNode!["contentCoverImage"] != null)
                                        {
                                            record.detailImage.contentCoverImage = detailImageNode!["contentCoverImage"]!.ToString();
                                        }
                                    }
                                    catch { }
                                    try
                                    {
                                        if (detailImageNode!["endCoverImage"] != null)
                                        {
                                            record.detailImage.endCoverImage = detailImageNode!["endCoverImage"]!.ToString();
                                        }
                                    }
                                    catch { }
                                }
                                try
                                {
                                    if (recordNode["payType"] != null)
                                    {
                                        record.payType = recordNode["payType"]!.ToString();
                                    }
                                }
                                catch { }
                                this.data.records.Add(record);
                            }
                        }
                        try
                        {
                            if (dataNode["pageNum"] != null)
                            {
                                this.data.pageNum = Convert.ToInt64(dataNode["pageNum"]!.ToString());
                            }
                        }
                        catch { }
                    }
                    try
                    {
                        if (jsonNode["sid"] != null)
                        {
                            this.sid = jsonNode["sid"]!.ToString();
                        }
                    }
                    catch { }
                }
            }
            catch { }
        }
    }

        /// <summary>
        /// 模板数据类，包含 PPT 主题列表的总数和具体列表集合。
        /// Template data class, containing the total number and specific list collection of the PPT theme list.
        /// </summary>
        public class TemplateData
        {
            /// <summary>
            /// PPT 主题列表总数，统计列表中主题的数量。
            /// Total number of PPT themes, counting the number of themes in the list.
            /// </summary>
            public long? total { get; set; }

            /// <summary>
            /// PPT 主题列表集合，存储具体的模板记录。
            /// Collection of PPT theme lists, storing specific template records.
            /// </summary>
            public List<TemplateRecord>? records { get; set; }

            /// <summary>
            /// 当前页码，指示当前显示的是第几页数据。
            /// Current page number, indicating which page of data is currently being displayed.
            /// </summary>
            public long? pageNum { get; set; }
        }

        /// <summary>
        /// 模板记录类，包含单个 PPT 主题的详细信息。
        /// Template record class, containing detailed information of a single PPT theme.
        /// </summary>
        public class TemplateRecord
        {
            /// <summary>
            /// 供用户检索模板的 ID，用于唯一标识一个模板。
            /// ID for users to retrieve templates, used to uniquely identify a template.
            /// </summary>
            public string? templateIndexId { get; set; }

            /// <summary>
            /// 总页数，该 PPT 主题包含的页面数量。
            /// Total number of pages, the number of pages contained in this PPT theme.
            /// </summary>
            public long? pageCount { get; set; }

            /// <summary>
            /// 类型，表明该 PPT 主题的类型。
            /// Type, indicating the type of this PPT theme.
            /// </summary>
            public string? type { get; set; }

            /// <summary>
            /// 颜色类型，该 PPT 主题的主要颜色风格。
            /// Color type, the main color style of this PPT theme.
            /// </summary>
            public string? color { get; set; }

            /// <summary>
            /// 行业类型，该 PPT 主题适用的行业领域。
            /// Industry type, the industry field applicable to this PPT theme.
            /// </summary>
            public string? industry { get; set; }

            /// <summary>
            /// 风格类型，该 PPT 主题的设计风格。
            /// Style type, the design style of this PPT theme.
            /// </summary>
            public string? style { get; set; }

            /// <summary>
            /// 详细图，包含该 PPT 主题的各种封面图片信息。
            /// Detailed images, containing information about various cover images of this PPT theme.
            /// </summary>
            public TemplateDetailImage? detailImage { get; set; }

            /// <summary>
            /// 模板支付方式，注意：所有模板均已免费使用。
            /// Template payment method. Note: All templates are now free to use.
            /// </summary>
            public string? payType { get; set; }
        }

        /// <summary>
        /// 模板详细图类，包含 PPT 主题各种封面图片的链接。
        /// Template detailed image class, containing links to various cover images of the PPT theme.
        /// </summary>
        public class TemplateDetailImage
        {
            /// <summary>
            /// 大标题封面图片链接，展示标题的大图。
            /// Link to the large title cover image, showing a large image of the title.
            /// </summary>
            public string? titleCoverImageLarge { get; set; }

            /// <summary>
            /// 标题封面图片链接，常规大小的标题封面图。
            /// Link to the title cover image, a regular-sized title cover image.
            /// </summary>
            public string? titleCoverImage { get; set; }

            /// <summary>
            /// 目录封面图片链接，用于展示目录的封面图。
            /// Link to the catalogue cover image, used to display the cover image of the catalogue.
            /// </summary>
            public string? catalogueCoverImage { get; set; }

            /// <summary>
            /// 章节封面图片链接，展示章节的封面图。
            /// Link to the chapter cover image, showing the cover image of the chapter.
            /// </summary>
            public string? chapterCoverImage { get; set; }

            /// <summary>
            /// 内容封面图片链接，用于展示内容部分的封面图。
            /// Link to the content cover image, used to display the cover image of the content section.
            /// </summary>
            public string? contentCoverImage { get; set; }

            /// <summary>
            /// 结束封面图片链接，展示结束页的封面图。
            /// Link to the end cover image, showing the cover image of the end page.
            /// </summary>
            public string? endCoverImage { get; set; }
        }
    
}
